package com.example.kotlin.download

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.example.kotlin.R
import com.example.kotlin.showToast

class DownloadService : Service() {
    private var downloadTask: DownloadTask? = null

    private var downloadUrl: String? = null

    private var downloadListener: DownloadListener? = object : DownloadListener {
        override fun onProgress(process: Int) {
            getNotificationManager().notify(1, getNotification("正在下载......", process))
        }

        override fun onSuccess() {

            downloadTask = null
            //下载成功后开启一个前台通知
            stopForeground(true)
            getNotificationManager().notify(1, getNotification("下载成功", -1))
            showToast("下载成功")

        }

        override fun onFailed() {
            downloadTask = null
            stopForeground(true)
            getNotificationManager().notify(1, getNotification("下载失败", -1))
            showToast("下载失败")

        }

        override fun onPause() {
            downloadTask = null
            stopForeground(true)
            showToast("下载暂停")

        }

        override fun onCancelled() {
            downloadTask = null
            stopForeground(true)
            showToast("下载取消")

        }

    }

    private val mBinder: DownloadBinder by lazy {
        DownloadBinder(downloadTask, downloadUrl, downloadListener, this)
    }

    override fun onBind(intent: Intent): IBinder {
        return mBinder

    }

//    class DownloadBinder : Binder() {
//        fun startDownload(url:String){
//            if ()
//
//        }
//
//    }

    private fun getNotificationManager() =
        getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    private fun getNotification(title: String, progress: Int): Notification {
        val channelId = "default"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            val channel =
                NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT)
            val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            nm?.let {
                if (it.getNotificationChannel(channelId) == null) {//没有创建
                    it.createNotificationChannel(channel)//则先创建
                }
            }
        }
        val intent = Intent(this, DownloadActivity::class.java)
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(this,channelId)
        builder.setSmallIcon(R.mipmap.ic_launcher)
        builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
        builder.setContentIntent(pendingIntent)
        builder.setContentTitle(title)

        if (progress > 0) {
            builder.setContentText("$progress%")
            builder.setProgress(100, progress, false)
        }
        return builder.build()


    }
}
